package com.si.anddos;

import com.si.anddos.dpadapters.DatabaseProvider;
import com.si.anddos.library.main.UploadProviderThread;

import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.IBinder;
import android.widget.Toast;

public class UploadProviderService extends Service {

	private DatabaseProvider dbProvider;
	private WifiLock lock;
	private UploadProviderThread upload;
	public static boolean running = false;
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		
		this.dbProvider = new DatabaseProvider(this.getApplicationContext());
		this.upload = new UploadProviderThread(this.dbProvider, this);
		this.upload.start();
		
		UploadProviderService.running = true;
		this.displayInformation(this.getText(R.string.uploadServiceStarted).toString());

		WifiManager wm = (WifiManager)getSystemService(WIFI_SERVICE);
		this.lock = wm.createWifiLock("UploadWifiLock");
		this.lock.acquire();
	}

	public void displayInformation(String text) {
		Toast.makeText(this.getApplicationContext(), text, Toast.LENGTH_SHORT)
				.show();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		UploadProviderService.running = false;
		this.lock.release();
		this.displayInformation(this.getText(R.string.uploadServiceStoped).toString());
	}				
}
